#include "CameraControler.h"
#include <math.h>



CameraControler::CameraControler(wiimote *remote, ICameraSceneNode *camera){
    m_remote = remote;
    m_camera = camera;
    m_camera -> bindTargetAndRotation(true);

}



void CameraControler::update(){
    up = m_remote->Button.Up();
    down = m_remote->Button.Down();
    left = m_remote->Button.Left();
    right = m_remote->Button.Right();

    vector3df v = m_camera->getRotation ();
    vector3df v2 = m_camera->getAbsolutePosition();
    matrix4 m = m_camera->getAbsoluteTransformation();

    if (!m_remote->Button.B()){

    if (m_remote->Acceleration.Orientation.Roll <= -10 | m_remote->Acceleration.Orientation.Roll >= 10)
        m_camera->setRotation(vector3df(v.X,v.Y+m_remote->Acceleration.Orientation.Roll/500,v.Z));

    if ((m_remote->Acceleration.Orientation.Pitch <= -10 & m_remote->Acceleration.Orientation.Pitch >= -90)
      | (m_remote->Acceleration.Orientation.Pitch >=  10 & m_remote->Acceleration.Orientation.Pitch <=  90))
        m_camera->setRotation(vector3df(v.X-m_remote->Acceleration.Orientation.Pitch/500,v.Y,v.Z));

    }

    if (up & left)
    {
        matrix4 front_left_move;
        front_left_move.setTranslation(vector3df (-0.1f,0,0.1f));
        m_camera->setPosition((m*front_left_move).getTranslation());
        return;

    }

    if (up & right)
    {
        matrix4 front_right_move;
        front_right_move.setTranslation(vector3df (0.1f,0,0.1f));
        m_camera->setPosition((m*front_right_move).getTranslation());
        return;

    }

    if (down & left)
    {
        matrix4 back_left_move;
        back_left_move.setTranslation(vector3df (-0.1f,0,-0.1f));
        m_camera->setPosition((m*back_left_move).getTranslation());
        return;

    }

    if (down & right)
    {
        matrix4 back_right_move;
        back_right_move.setTranslation(vector3df (0.1f,0,-0.1f));
        m_camera->setPosition((m*back_right_move).getTranslation());
        return;

    }

    if (up)
    {
        matrix4 front_move;
        front_move.setTranslation(vector3df (0,0,0.1f));
        m_camera->setPosition((m*front_move).getTranslation());
        return;
    }

    if (down)
    {
        matrix4 back_move;
        back_move.setTranslation(vector3df (0,0,-0.1f));
        m_camera->setPosition((m*back_move).getTranslation());
        return;
    }
    if (left)
    {
        matrix4 left_move;
        left_move.setTranslation(vector3df (-0.1f,0,0));
        m_camera->setPosition((m*left_move).getTranslation());
    }
    if (right)
    {
        matrix4 right_move;
        right_move.setTranslation(vector3df (0.1f,0,0));
        m_camera->setPosition((m*right_move).getTranslation());
        return;
    }

    if (m_remote->Acceleration.Y < -0.450)
    {
        matrix4 up_move;
        up_move.setTranslation(vector3df (0,0.1f,0));
        m_camera->setPosition((m*up_move).getTranslation());
    }

    if (m_remote->Acceleration.Y > 0.300)
    {
        matrix4 down_move;
        down_move.setTranslation(vector3df (0,-0.1f,0));
        m_camera->setPosition((m*down_move).getTranslation());
    }


}
